package com.digital.welcome.util;


import org.springframework.stereotype.Component;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class TemplateUtil {
    public TemplateUtil setTemplate(String template) {
        this.template = template;
        return new TemplateUtil(template);
    }

    String template;
    final Pattern pattern = Pattern.compile("\\$\\{(\\w*)\\}");

    public TemplateUtil() {

    }
    public TemplateUtil(String template) {
        this.template = template;
    }

    public String formatted(Object... data) {
        int i = 0;
        Matcher m = pattern.matcher(template);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            m.appendReplacement(sb, data[i].toString());
            i++;
        }
        m.appendTail(sb);
        return sb.toString();
    }
}
